package net.thxjj.adminServer.mongodao;

import net.thxjj.adminServer.entity.StaticBean;
import net.thxjj.adminServer.entity.aep.ProfileLog;
import net.thxjj.adminServer.entity.aep.profilegc.ProfileGCInfoBean;
import net.thxjj.adminServer.util.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MongoProfileGCInfoDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    public void saveProfileGCInfoBean(ProfileGCInfoBean profileGCInfoBean){
        ProfileGCInfoBean po = findGCInfoBeanByDeviceIdAndTime(profileGCInfoBean.getDeviceId(),profileGCInfoBean.getTimestamp());
        if(po==null){
            mongoTemplate.save(profileGCInfoBean);
        }else{
            updateGCInfoBean(profileGCInfoBean);
        }
    }

    //导入校验（如果有timestamp和deviceId重合则更新）
    public void saveProfileGCInfoBeans(List<ProfileGCInfoBean> list){
        for (ProfileGCInfoBean profile:
                list) {
            ProfileGCInfoBean po = findGCInfoBeanByDeviceIdAndTime(profile.getDeviceId(),profile.getTimestamp());
            if(po==null){
                mongoTemplate.save(profile);
            }else{
                updateGCInfoBean(profile);
            }
        }
    }

    private ProfileGCInfoBean findGCInfoBeanByDeviceIdAndTime(String deviceId,long time){
        Query query = new Query(Criteria.where("deviceId").is(deviceId).and("timestamp").is(time));
        return mongoTemplate.findOne(query,ProfileGCInfoBean.class);
    }

    private void updateGCInfoBean(ProfileGCInfoBean profileGCInfoBean) {

        Query query = new Query(Criteria.where("deviceId").is(profileGCInfoBean.getDeviceId()).and("timestamp").is(profileGCInfoBean.getTimestamp()));
        Update update = new Update()
                .set("TAC",profileGCInfoBean.getTAC())
                .set("SNR",profileGCInfoBean.getSNR())
                .set("RSRQ",profileGCInfoBean.getRSRQ())
                .set("RSRP",profileGCInfoBean.getRSRP())
                .set("PCI",profileGCInfoBean.getPCI())
                .set("ECL",profileGCInfoBean.getECL())
                .set("EARFCN",profileGCInfoBean.getEARFCN())
                .set("CSQ",profileGCInfoBean.getCSQ())
                .set("Protocol_Number",profileGCInfoBean.getProtocol_Number())
                .set("Custom_Msg",profileGCInfoBean.getCustom_Msg())
                .set("Custom_Msg_Length",profileGCInfoBean.getCustom_Msg_Length());
        mongoTemplate.upsert(query,update,ProfileGCInfoBean.class);

    }


    public ProfileGCInfoBean getGCInfoStatus(String deviceId){
        Query query = new Query(Criteria.where("deviceId").is(deviceId));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.findOne(query,ProfileGCInfoBean.class);
    }


    public List<ProfileGCInfoBean> getAllinfoBeans(String deviceId){
        Query query = new Query(Criteria.where("deviceId").is(deviceId));
        query.with(new Sort(Sort.Direction.ASC,"timestamp"));
        return mongoTemplate.find(query,ProfileGCInfoBean.class);
    }

    public List<ProfileGCInfoBean> getInfoByTime(String deviceId, long start_time, long end_time){
        Query query = new Query(Criteria.where("deviceId").is(deviceId).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time)));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.find(query,ProfileGCInfoBean.class);
    }


    public StaticBean getEarfcnStatic(String device_id, long start_time, long end_time){
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time))),
                Aggregation.group().max("EARFCN").as("max").min("EARFCN").as("min").avg("EARFCN").as("avg"));

        StaticBean staticBean =  mongoTemplate.aggregate(aggregation,"th_profile_gc_info_log", StaticBean.class).getUniqueMappedResult();
        //平均值保留两位小数
        if(staticBean!=null){
            String dea = DataUtils.getDoubleString(staticBean.getAvg());
            staticBean.setAvg(dea);
        }else {
            staticBean = new StaticBean().setMin("").setMax("").setAvg("");
        }
        return staticBean;
    }

    public StaticBean getSnrStatic(String device_id, long start_time, long end_time){
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time))),
                Aggregation.group().max("SNR").as("max").min("SNR").as("min").avg("SNR").as("avg"));

        StaticBean staticBean =  mongoTemplate.aggregate(aggregation,"th_profile_gc_info_log", StaticBean.class).getUniqueMappedResult();
        //平均值保留两位小数
        if(staticBean!=null){
            String dea = DataUtils.getDoubleString(staticBean.getAvg());
            staticBean.setAvg(dea);
        }else {
            staticBean = new StaticBean().setMin("").setMax("").setAvg("");
        }
        return staticBean;
    }

    public StaticBean getRsrpStatic(String device_id, long start_time, long end_time){
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time))),
                Aggregation.group().max("RSRP").as("max").min("RSRP").as("min").avg("RSRP").as("avg"));

        StaticBean staticBean =  mongoTemplate.aggregate(aggregation,"th_profile_gc_info_log", StaticBean.class).getUniqueMappedResult();
        //平均值保留两位小数
        if(staticBean!=null){
            String dea = DataUtils.getDoubleString(staticBean.getAvg());
            staticBean.setAvg(dea);
        }else {
            staticBean = new StaticBean().setMin("").setMax("").setAvg("");
        }
        return staticBean;
    }

    public StaticBean getRsrqStatic(String device_id, long start_time, long end_time){
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time))),
                Aggregation.group().max("RSRQ").as("max").min("RSRQ").as("min").avg("RSRQ").as("avg"));

        StaticBean staticBean =  mongoTemplate.aggregate(aggregation,"th_profile_gc_info_log", StaticBean.class).getUniqueMappedResult();
        //平均值保留两位小数
        if(staticBean!=null){
            String dea = DataUtils.getDoubleString(staticBean.getAvg());
            staticBean.setAvg(dea);
        }else {
            staticBean = new StaticBean().setMin("").setMax("").setAvg("");
        }
        return staticBean;
    }

    public StaticBean getCsqStatic(String device_id, long start_time, long end_time){
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time))),
                Aggregation.group().max("CSQ").as("max").min("CSQ").as("min").avg("CSQ").as("avg"));

        StaticBean staticBean =  mongoTemplate.aggregate(aggregation,"th_profile_gc_info_log", StaticBean.class).getUniqueMappedResult();
        //平均值保留两位小数
        if(staticBean!=null){
            String dea = DataUtils.getDoubleString(staticBean.getAvg());
            staticBean.setAvg(dea);
        }else {
            staticBean = new StaticBean().setMin("").setMax("").setAvg("");
        }
        return staticBean;
    }

    public StaticBean getPciStatic(String device_id, long start_time, long end_time){
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time))),
                Aggregation.group().max("PCI").as("max").min("PCI").as("min").avg("PCI").as("avg"));

        StaticBean staticBean =  mongoTemplate.aggregate(aggregation,"th_profile_gc_info_log", StaticBean.class).getUniqueMappedResult();
        //平均值保留两位小数
        if(staticBean!=null){
            String dea = DataUtils.getDoubleString(staticBean.getAvg());
            staticBean.setAvg(dea);
        }else {
            staticBean = new StaticBean().setMin("").setMax("").setAvg("");
        }
        return staticBean;
    }

    public StaticBean getTacStatic(String device_id, long start_time, long end_time){
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time))),
                Aggregation.group().max("TAC").as("max").min("TAC").as("min").avg("TAC").as("avg"));

        StaticBean staticBean =  mongoTemplate.aggregate(aggregation,"th_profile_gc_info_log", StaticBean.class).getUniqueMappedResult();
        //平均值保留两位小数
        if(staticBean!=null){
            String dea = DataUtils.getDoubleString(staticBean.getAvg());
            staticBean.setAvg(dea);
        }else {
            staticBean = new StaticBean().setMin("").setMax("").setAvg("");
        }
        return staticBean;
    }

    public StaticBean getEclStatic(String device_id, long start_time, long end_time){
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time))),
                Aggregation.group().max("ECL").as("max").min("ECL").as("min").avg("ECL").as("avg"));

        StaticBean staticBean =  mongoTemplate.aggregate(aggregation,"th_profile_gc_info_log", StaticBean.class).getUniqueMappedResult();
        //平均值保留两位小数
        if(staticBean!=null){
            String dea = DataUtils.getDoubleString(staticBean.getAvg());
            staticBean.setAvg(dea);
        }else {
            staticBean = new StaticBean().setMin("").setMax("").setAvg("");
        }
        return staticBean;
    }

}
